/**
 *
 * Copyright 2011 TeleNav, Inc. All rights reserved.
 * ThumbnailActivity.java
 *
 */
package com.palmwindActivity.thumbnail;

import java.util.Vector;

import com.palmwindActivity.R;
import com.palmwindActivity.Gallery.GalleryActivity;
import com.palmwindActivity.app.AbstractActivity;
import com.palmwindActivity.app.AbstractCommManager;
import com.palmwindActivity.app.AbstractServiceProxy;
import com.palmwindActivity.app.main.DemoCommManager;
import com.palmwindActivity.app.main.ImageServiceProxy;
import com.palmwindActivity.app.main.MenuHandler;
import com.palmwindActivity.app.main.ThreadPoolManager;
import com.palmwindActivity.app.main.LocalThumbnailAdapter;
import com.palmwindActivity.widget.ShellGridView;
import com.palmwindActivity.app.IServiceProxyListener;


import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.Toast;

/**
 *@author Kane (gongchen.cn@gmail.com)
 *@date 2011-4-6
 */
public class ThumbnailActivity extends AbstractActivity implements OnClickListener ,IServiceProxyListener
{
    GridView gridView;
    LocalThumbnailAdapter adapter;
    int viewId;
    
    public int getViewId()
    {
        return viewId;
    }

    public void setViewId(int viewId)
    {
        this.viewId = viewId;
    }

    public LocalThumbnailAdapter getAdapter()
    {
        return adapter;
    }

    public void setAdapter(LocalThumbnailAdapter adapter)
    {
        this.adapter = adapter;
    }

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if (AbstractCommManager.getInstance() == null) {
			AbstractCommManager.init(new DemoCommManager(this));
		}
		adapter = new LocalThumbnailAdapter(this);
		gridView = new ShellGridView(this);
		adapter.setOnClickListener(this);

		gridView.setSmoothScrollbarEnabled(true);
		gridView.setColumnWidth(60);
		gridView.setNumColumns(GridView.AUTO_FIT);
		final Activity activity = (Activity) this;
		activity.runOnUiThread(new Runnable() {

			public void run() {
				gridView.setAdapter(adapter);

				FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
						FrameLayout.LayoutParams.FILL_PARENT,
						FrameLayout.LayoutParams.FILL_PARENT);
				gridView.setLayoutParams(params);
				setContentView(gridView);

			}
		});

	}

    public void onClick(View v)
    {
        this.viewId = v.getId();
        Intent intent = new Intent(this, GalleryActivity.class);
        intent.putExtra("ViewId", viewId);
        startActivity(intent);
    }

    public void networkError(int errorCode, String actionId, String errorMessage, AbstractServiceProxy proxy)
    {
        System.out.println("Network error");
        this.setIsProgressBarShown(false);
        
        final Activity activity = (Activity) this;
        activity.runOnUiThread(new Runnable() {

            public void run() {
                gridView.setAdapter(adapter);
                Toast.makeText(activity, R.string.network_error, 2000).show();
            }

        });
        
    }

    public void networkFinished(int status, String actionId, final AbstractServiceProxy proxy)
    {
        System.out.println("Network finished");
        this.setIsProgressBarShown(false);
        final Activity activity = (Activity) this;
        activity.runOnUiThread(new Runnable()
        {
            public void run()
            {
                if (proxy instanceof ImageServiceProxy)
                {
                    ImageServiceProxy imageServiceProxy = (ImageServiceProxy) proxy;
                    Vector<Bitmap> imageVector = imageServiceProxy.getImages();
                    adapter.setNetWorkData(imageVector);
                }

                gridView.setAdapter(adapter);
            }

        });

    }
    
    private void requestImages()
    {
        ImageServiceProxy proxy = new ImageServiceProxy(ThreadPoolManager.getInstance().getThreadPool(ThreadPoolManager.NET_WORK), this);
        proxy.requestImage(System.currentTimeMillis());
    }
    
    private synchronized void setIsProgressBarShown(final boolean isShown)
    {
        final Activity activity = (Activity) this;
        activity.runOnUiThread(new Runnable() {

            public void run() {
                activity.setProgressBarIndeterminateVisibility(isShown);
            }

        });
    }
    
    public boolean onCreateOptionsMenu (Menu menu)
    {
        MenuItem textMenu = menu.add(0, MenuHandler.TEXT_ID, Menu.NONE, R.string.text_input);
        MenuHandler menuHandler = new MenuHandler(this);
        textMenu.setOnMenuItemClickListener(menuHandler);
        return super.onCreateOptionsMenu(menu);
    }

	protected void onStartDelegate() {
		// TODO Auto-generated method stub
		
	}
	
	public void onBackPressed()
	{
		SharedPreferences preference = getSharedPreferences("releaseall", 0);
    	Editor edit = preference.edit();
    	edit.putBoolean("isRelease", true);
    	edit.commit();
    	super.onBackPressed();
	}
}
